﻿#include <iostream>
#include <opencv2/opencv.hpp>
#include <filesystem>
#include <opencv2/features2d.hpp>
#include "Planet.h"
#include <cmath>

using namespace cv;
using namespace std;
namespace fs = std::filesystem;

int main()
{
	// 获取当前可执行文件的路径
	fs::path exePath = fs::current_path();
	// 构建资源文件的完整路径
	fs::path imagePath = exePath / "x64\\Debug\\img\\C++.png";
	string imgPath_str = imagePath.generic_string();


	//加载图片: 图片路径、读取图像的方式
	Mat image = imread(imgPath_str, IMREAD_COLOR);
	//可调整窗体大小
	//namedWindow("图片显示窗口", WINDOW_FREERATIO);

	if (image.empty())
	{
		cout << "没有图像\n";
		return -1;
	}
	//显示图像
	imshow("原图片窗口", image);







	// 视频参数设置
	int fps = 30;
	int total_frames = 10 * fps; // 10秒视频
	Size frame_size(800, 600);

	// 创建视频写入对象
	VideoWriter video("mars_impact.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, frame_size);

	if (!video.isOpened()) {
		cout << "无法创建视频文件!" << endl;
		return -1;
	}

	// 创建行星对象
	Planet earth(Point(600, 300), 80, Scalar(100, 100, 200)); // 蓝色地球
	Planet mars(Point(100, 300), 40, Scalar(200, 100, 100));  // 红色火星

	// 火星移动参数
	double mars_speed = 15.0; // 每帧移动的像素数
	double impact_frame = 150; // 碰撞发生的帧数

	// 爆炸效果参数
	vector<Point> explosion_particles;
	vector<Point> explosion_velocities;

	for (int i = 0; i < total_frames; i++) {
		Mat frame(frame_size, CV_8UC3, Scalar(0, 0, 0)); // 黑色背景

		// 添加一些星星
		for (int j = 0; j < 100; j++) {
			Point star(rand() % frame_size.width, rand() % frame_size.height);
			circle(frame, star, 1, Scalar(255, 255, 255), -1);
		}

		// 更新火星位置
		if (i < impact_frame) {
			double progress = static_cast<double>(i) / impact_frame;
			mars.center.x = 100 + progress * (earth.center.x - 100 - earth.radius - mars.radius);
			mars.center.y = 300 + sin(progress * CV_PI) * 100; // 稍微弯曲的轨迹
		}

		// 绘制行星
		earth.draw(frame);
		mars.draw(frame);

		// 处理碰撞效果
		if (i >= impact_frame) {
			double explosion_progress = (i - impact_frame) / (total_frames - impact_frame);

			// 初始化爆炸粒子
			if (i == impact_frame) {
				Point impact_point = earth.center;
				impact_point.x -= earth.radius;

				for (int j = 0; j < 200; j++) {
					explosion_particles.push_back(impact_point);
					double angle = (rand() % 360) * CV_PI / 180.0;
					double speed = 1 + rand() % 10;
					explosion_velocities.push_back(Point(
						speed * cos(angle),
						speed * sin(angle)
					));
				}
			}

			// 更新和绘制爆炸粒子
			for (size_t j = 0; j < explosion_particles.size(); j++) {
				explosion_particles[j] += explosion_velocities[j];
				explosion_velocities[j] *= 0.95; // 减速

				// 根据速度改变颜色（从红色到橙色到黄色）
				Scalar particle_color;
				double speed = norm(explosion_velocities[j]);
				if (speed > 5) particle_color = Scalar(0, 0, 255); // 红色
				else if (speed > 2) particle_color = Scalar(0, 165, 255); // 橙色
				else particle_color = Scalar(0, 255, 255); // 黄色

				circle(frame, explosion_particles[j], 2, particle_color, -1);
			}

			// 绘制冲击波
			int wave_radius = static_cast<int>(explosion_progress * 300);
			circle(frame, earth.center, wave_radius, Scalar(255, 255, 255), 2);
		}

		// 添加文字说明
		putText(frame, "Mars Impact Simulation", Point(20, 30),
			FONT_HERSHEY_SIMPLEX, 0.7, Scalar(255, 255, 255), 2);

		// 写入视频帧
		video.write(frame);

		// 可选：显示实时预览
		imshow("Mars Impact", frame);
		if (waitKey(30) >= 0) break;
	}

	video.release();
	cout << "视频已生成: mars_impact.avi" << endl;




	waitKey();
	return 0;
}
